FROM {{ namespace }}/{{ image_prefix }}base:{{ tag }}
{% block labels %}
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
{% endblock %}

{% block kolla_toolbox_header %}{% endblock %}

{% set virtualenv_path='/opt/ansible' %}
{% set os_client_config= virtualenv_path + '/lib/python' + distro_python_version + '/site-packages/os_client_config/defaults.json' %}

{% import "macros.j2" as macros with context %}

{{ macros.configure_user(name='ansible') }}
{{ macros.configure_user(name='rabbitmq') }}

{{ macros.enable_extra_repos(['rabbitmq', 'erlang']) }}

{% if base_package_type == 'rpm' %}
    {% set kolla_toolbox_packages = [
        'crudini',
        'gcc',
        'gdisk',
        'git',
        'jq',
        'libffi-devel',
        'libxml2-devel',
        'libxslt-devel',
        'make',
        'mariadb',
        'mariadb-devel',
        'openssh-clients',
        'openssl-devel',
        'python3-devel',
        'rabbitmq-server-3.8.*'
        ] %}

{% elif base_package_type == 'deb' %}
    {% set kolla_toolbox_packages = [
        'build-essential',
        'ca-certificates',
        'crudini',
        'gdisk',
        'git',
        'jq',
        'libffi-dev',
        'libmariadb-dev-compat',
        'libssl-dev',
        'libxslt1-dev',
        'mariadb-client',
        'python3-dev',
        'rabbitmq-server',
     ] %}
{% endif %}
{{ macros.install_packages(kolla_toolbox_packages | customizable("packages")) }}

{% block kolla_toolbox_source_install_python_pip %}
{{ macros.get_pip() }}
{% endblock %}

{% set kolla_toolbox_pip_virtualenv_packages = [
        'virtualenv'
    ] %}

RUN {{ macros.install_pip(kolla_toolbox_pip_virtualenv_packages | customizable("pip_virtualenv_packages"), constraints=false) }} \
    && virtualenv --system-site-packages {{ virtualenv_path }}

ENV PATH {{ virtualenv_path }}/bin:$PATH

# NOTE(mgoddard): Ansible 2.8.9 broke OpenStack modules.
# https://bugs.launchpad.net/kolla/+bug/1866181
{% set kolla_toolbox_pip_packages = [
        'ansible==2.8.*,!=2.8.9',
        'cmd2',
        'influxdb',
        'mysqlclient',
        'os-client-config',
        'pbr',
        'pymongo',
        'python-openstackclient',
        'pytz',
        'pyudev',
        'shade'
    ] %}

RUN mkdir -p /requirements \
    && curl -sSL -o /requirements/upper-constraints.txt https://releases.openstack.org/constraints/upper/{{ openstack_release }} \
    && {{ macros.install_pip(kolla_toolbox_pip_packages | customizable("pip_packages")) }} \
    && mkdir -p /etc/ansible /usr/share/ansible \
    && echo 'localhost ansible_connection=local ansible_python_interpreter={{ virtualenv_path }}/bin/python' > /etc/ansible/hosts \
    && sed -i 's|  "identity_api_version": "2.0",|  "identity_api_version": "3",|' {{ os_client_config }}

ENV ANSIBLE_LIBRARY /usr/share/ansible:$ANSIBLE_LIBRARY

COPY find_disks.py kolla_sanity.py /usr/share/ansible/
COPY ansible.cfg /var/lib/ansible/.ansible.cfg
RUN chmod 644 /usr/share/ansible/find_disks.py \
              /usr/share/ansible/kolla_sanity.py \
              /var/lib/ansible/.ansible.cfg

COPY ansible_sudoers /etc/sudoers.d/kolla_ansible_sudoers
RUN chmod 440 /etc/sudoers.d/kolla_ansible_sudoers

{% block kolla_toolbox_footer %}{% endblock %}
{% block footer %}{% endblock %}

USER ansible
